package com.chester.quickstarter.util.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author chester7cheng
 */
public final class EncryptUtil {
    private final static Logger logger = LoggerFactory.getLogger(EncryptUtil.class);

//    public static Object md5HashEncrypt(String value) {
//        ByteSource salt = Md5Hash.Util.bytes("www.focus.com-20170105-chestercheng");
//        Object credentials = new Md5Hash(value, salt, 1024);
//        logger.debug(credentials.toString());
//        return credentials;
//    }

    public static String md5Base64Encrypt(String value) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        // 计算md5函数
        md.update(value.getBytes());
        // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
        // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值

        String encryptValue = new BigInteger(1, md.digest()).toString(16);
        logger.debug("encryptValue: " + encryptValue);
        return encryptValue.toString();
    }
}
